<template>
  <div class="tab-content status-overview">
    <template v-if="statusCode != '111510'">
      <el-scrollbar>
        <div class="top-card-container">
          <dataCard :dataArr="cardList" :code="statusCode"></dataCard>
        </div>
      </el-scrollbar>
    </template>
    <div v-if="statusCode != '111510'" class="middle-filter-container">
      <div class="left-item">
        <div v-if="!type" class="variety">
          {{ $t('custom.品种') }}：
          <el-radio-group v-model="varietyId">
            <el-radio value="全部" size="large">全部</el-radio>
            <el-radio :label="item.materialName" v-for="(item, index) in zaiFangList" :key="index">
              {{ item.materialName }}
            </el-radio>
          </el-radio-group>
        </div>
        <div v-else class="efficiency">
          {{ $t('custom.效率最低') }}
          <el-input-number
            class="number-input"
            v-model="number"
            :min="1"
            :max="500"
            :controls="false"
            :placeholder="$t('custom.请输入内容')"
            @change="filterChange"
          />
          台车</div
        >
      </div>
      <div class="right-item">
        <el-switch
          v-model="type"
          style="--el-switch-on-color: #368aff; --el-switch-off-color: #13ce66"
          active-text="效率"
          inactive-text="品种"
        />
      </div>
    </div>
    <div
      class="main-content"
      v-load="loading"
      :style="{ height: statusCode != '111510' ? 'calc(100vh - 346px)' : '100%' }"
    >
      <el-row>
        <el-col
          :sm="12"
          :md="12"
          :lg="8"
          :xl="6"
          v-for="(item, index) in mainCardList"
          :key="index"
        >
          <mainDataCard :data="item" :code="statusCode" :process-name="processName"></mainDataCard>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script setup lang="ts">
  import { dataRegroup } from '../utils'
  import dataCard from '../../businessComponents/dataCard.vue'
  import mainDataCard from '../../businessComponents/mainDataCard.vue'
  import { MainDataCardType } from '../types'

  import {
    getZhuangTaiZongLanAndTongJi_xs,
    getZhuangTaiZongLanAndTongJi_cs,
    getZhuangTaiZongLanAndTongJi_mb,
    getZhuangTaiZongLanAndTongJi_yb,
    getZhuangTaiZongLanAndTongJi_sm,
    getZhuangTaiZongLian_lvChen
  } from '@/api/shengChanJianShi'

  const props = defineProps({
    statusCode: {
      type: String,
      default: '11310'
    },
    processName: {
      type: String,
      default: ''
    }
  })

  console.log(props)

  const loading = ref(false)
  const cardList = ref<any[]>([])
  const backup = ref<any>()
  const mainCardList = ref<MainDataCardType[]>([])
  const zaiFangList = ref<any[]>([])
  const number = ref()
  const type = ref(true)
  const varietyId = ref('全部')

  const filterChange = (value: number | undefined) => {
    mainCardList.value = []
    if (!value) {
      nextTick(() => {
        mainCardList.value = backup.value
      })
    } else {
      mainCardList.value = backup.value
        .sort(function (a, b) {
          return a.runEfc - b.runEfc
        })
        .slice(0, number.value)
    }
  }

  const getDataCardList = () => {
    loading.value = true
    switch (props.statusCode) {
      case '11310': // 细纱
        getSpunyarnList()
        break
      case '11410': // 粗纱
        getRovingList()
        break
      case '11510': // 末并
        getFinalMergingList()
        break
      case '11810': // 预并
        getPreDrawingList()
        break
      case '110910': // 梳棉
        getCottonCardingList()
        break
      case '111510': // 滤尘
        getFilterDustList()
        break
      default:
        break
    }
  }

  // 细纱页面数据
  const getSpunyarnList = () => {
    getZhuangTaiZongLanAndTongJi_xs()
      .then((res: any) => {
        loading.value = false
        handleData(res, 'machineOnlineXs')
      })
      .catch(() => {
        loading.value = false
      })
  }

  // 粗纱页面数据
  const getRovingList = () => {
    getZhuangTaiZongLanAndTongJi_cs()
      .then((res: any) => {
        loading.value = false
        handleData(res, 'machineOnlineCs')
      })
      .catch(() => {
        loading.value = false
      })
  }

  // 末并页面数据
  const getFinalMergingList = () => {
    getZhuangTaiZongLanAndTongJi_mb()
      .then((res: any) => {
        loading.value = false
        handleData(res, 'machineOnlineMb')
      })
      .catch(() => {
        loading.value = false
      })
  }

  // 预并页面数据
  const getPreDrawingList = () => {
    getZhuangTaiZongLanAndTongJi_yb()
      .then((res: any) => {
        loading.value = false
        handleData(res, 'machineOnlineYb')
      })
      .catch(() => {
        loading.value = false
      })
  }

  // 梳棉页面数据
  const getCottonCardingList = () => {
    getZhuangTaiZongLanAndTongJi_sm()
      .then((res: any) => {
        loading.value = false
        handleData(res, 'machineOnlineSm')
      })
      .catch(() => {
        loading.value = false
      })
  }

  // 滤尘页面数据
  const getFilterDustList = () => {
    getZhuangTaiZongLian_lvChen()
      .then((res: any) => {
        loading.value = false
        mainCardList.value = res
      })
      .catch(() => {
        loading.value = false
      })
  }

  const handleData = (res, key = '') => {
    loading.value = false
    cardList.value = dataRegroup(res)
    mainCardList.value = res[key]
    backup.value = res[key]
  }

  onMounted(() => {
    getDataCardList()
  })
</script>

<style scoped lang="scss">
  .status-overview {
    height: 100%;
  }
  .top-card-container {
    display: flex;
    margin-bottom: 20px;
  }
  .middle-filter-container {
    display: flex;
    justify-content: space-between;
    height: 64px;
    line-height: 64px;
    background-color: #fff;
    padding: 0 20px 0 24px;
    box-sizing: border-box;
    margin-bottom: 20px;

    .el-radio-group {
      vertical-align: middle;
    }

    .number-input {
      width: 100px;
    }
  }

  .main-content {
    // display: flex;
    flex-wrap: wrap;
    margin: 0 -12.5px;
    height: calc(100vh - 346px);
    overflow-y: auto;
  }
</style>
